package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Video;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("VideoDAO")
@Transactional
public class VideoDAOImpl extends AbstractJpaDao<Video> implements VideoDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Video.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public VideoDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Video findVideoById(Integer id) throws DataAccessException {

		return findVideoById(id, -1, -1);
	}

	@Transactional
	public Video findVideoById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findVideoById", startResult, maxRows, id);
			return (Video) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Video> findVideoByName(String name) throws DataAccessException {

		return findVideoByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Video> findVideoByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findVideoByName", startResult, maxRows, name);
		return new LinkedHashSet<Video>(query.getResultList());
	}

	@Transactional
	public Set<Video> findVideoByNameContaining(String name) throws DataAccessException {

		return findVideoByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Video> findVideoByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findVideoByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Video>(query.getResultList());
	}

	@Transactional
	public Set<Video> findVideoByPath(String path) throws DataAccessException {

		return findVideoByPath(path, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Video> findVideoByPath(String path, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findVideoByPath", startResult, maxRows, path);
		return new LinkedHashSet<Video>(query.getResultList());
	}

	@Transactional
	public Set<Video> findVideoByPathContaining(String path) throws DataAccessException {

		return findVideoByPathContaining(path, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Video> findVideoByPathContaining(String path, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findVideoByPathContaining", startResult, maxRows, path);
		return new LinkedHashSet<Video>(query.getResultList());
	}

	@Transactional
	public Set<Video> findVideoByDescription(String description) throws DataAccessException {

		return findVideoByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Video> findVideoByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findVideoByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Video>(query.getResultList());
	}

	@Transactional
	public Set<Video> findVideoByDescriptionContaining(String description) throws DataAccessException {

		return findVideoByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Video> findVideoByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findVideoByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Video>(query.getResultList());
	}

	@Transactional
	public Video findVideoByPrimaryKey(Integer id) throws DataAccessException {
		return findVideoByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Video findVideoByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findVideoByPrimaryKey", startResult, maxRows, id);
			return (Video) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Video> findVideosByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findVideosByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Video> findAllVideos() throws DataAccessException {

		return findAllVideos(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Video> findAllVideos(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllVideos", startResult, maxRows);
		return new LinkedHashSet<Video>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Video> findVideosByEvent(Integer eventId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllVideosByEvent", start, max, eventId);
		return new LinkedHashSet<Video>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Video> findVideosByFood(Integer foodId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllVideosByFood", start, max, foodId);
		return new LinkedHashSet<Video>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Video> findVideoByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Video>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Video> findVideosByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Video.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Video> videos = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return videos;
	}

	@Transactional
	public Video findVideoByPhysicalPrimaryKey(String name, String path) throws DataAccessException {
		return findVideoByPhysicalPrimaryKey(name, path, -1, -1);
	}

	@Transactional
	public Video findVideoByPhysicalPrimaryKey(String name, String path, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findVideoByPhyscialPrimaryKey", startResult, maxRows, name, path);
			return (Video) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Video entity) {
		return true;
	}
}
